package com.videoweb.service.VideoAuditService;

import com.videoweb.constant.AuditStatus;
import com.videoweb.entity.Video;
import com.videoweb.entity.response.AuditResponse;
import com.videoweb.service.IVideoService;
import com.videoweb.utils.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Service
public class PublishService implements InitializingBean {
    @Autowired
    ImageAuditService imageAuditService;

    @Autowired
    TextAuditService textAuditService;

    @Autowired
    VideoAuditService videoAuditService;

//    出现了循环依赖的问题
    @Lazy
    @Autowired
    IVideoService videoService;

    private int maximumPoolSize = 8;

    protected ThreadPoolExecutor executor;

//    public void audit(Video video,String coverUrl,String videoUrl){
//        executor.submit(() -> {
//
//        AuditResponse videoAuditResponse = new AuditResponse(AuditStatus.PASS,"正常");
//        AuditResponse coverAuditResponse = new AuditResponse(AuditStatus.PASS,"正常");
//        AuditResponse titleAuditResponse = new AuditResponse(AuditStatus.PASS,"正常");
//        AuditResponse descAuditResponse = new AuditResponse(AuditStatus.PASS,"正常");
//
////        开始审核啦：
//
//        videoAuditResponse = videoAuditService.audit(videoUrl);
//        coverAuditResponse = imageAuditService.audit(coverUrl);
//        titleAuditResponse = textAuditService.audit(video.getTitle());
//
//        if (!StringUtils.isEmpty(video.getDescription())){
//            descAuditResponse =textAuditService.audit(video.getDescription());
//        }
//
//        final Integer videoAuditStatus = videoAuditResponse.getAuditStatus();
//        final Integer coverAuditStatus = coverAuditResponse.getAuditStatus();
//        final Integer titleAuditStatus = titleAuditResponse.getAuditStatus();
//        final Integer descAuditStatus = descAuditResponse.getAuditStatus();
////            分别把得到的结果和成功的状态进行比较  看看是否都为真，   不为真的话就审核失败
//        boolean f1 = videoAuditStatus == AuditStatus.PASS;
//        boolean f2 = coverAuditStatus == AuditStatus.PASS;
//        boolean f3 = titleAuditStatus == AuditStatus.PASS;
//        boolean f4 = descAuditStatus == AuditStatus.PASS;
//
//        if (f1 && f2 && f3 && f4) {
//         videoService.updateStatusByVideoId(video.getId(),AuditStatus.PASS);
//            // 填充视频时长
//        }else {
////            TODO 要添加一个审核表来记录相应的信息呀
////
////            if (!f1){
////
////            }
////            if (!f2){
////
////            }
////            if (!f3){
////
////            }
////            if (!f4){
////
////            }
//            videoService.updateStatusByVideoId(video.getId(),AuditStatus.REVIEW);
//
//        }

    public void auditAsync(Video video, String coverUrl, String videoUrl) {
        executor.submit(() -> {
            try {
                // 执行审核逻辑
                AuditResponse videoAuditResponse = videoAuditService.audit(videoUrl);
                AuditResponse coverAuditResponse = imageAuditService.audit(coverUrl);
                AuditResponse titleAuditResponse = textAuditService.audit(video.getTitle());

                AuditResponse descAuditResponse = new AuditResponse(AuditStatus.PASS, "正常");
                if (!StringUtils.isEmpty(video.getDescription())) {
                    descAuditResponse = textAuditService.audit(video.getDescription());
                }

                // 判断审核结果
                boolean allPassed = videoAuditResponse.getAuditStatus() == AuditStatus.PASS &&
                        coverAuditResponse.getAuditStatus() == AuditStatus.PASS &&
                        titleAuditResponse.getAuditStatus() == AuditStatus.PASS &&
                        descAuditResponse.getAuditStatus() == AuditStatus.PASS;

                // 更新状态
                if (allPassed) {
                    videoService.updateStatusByVideoId(video.getId(), AuditStatus.PASS);
                } else {
                    videoService.updateStatusByVideoId(video.getId(), AuditStatus.REVIEW);
                }

            } catch (Exception e) {
                // 异常时设置为需要人工审核
                videoService.updateStatusByVideoId(video.getId(), AuditStatus.REVIEW);
            }
        });
    }



    @Override
    public void afterPropertiesSet() throws Exception {
        executor = new ThreadPoolExecutor(
                3,  // 核心线程数ThreadPoolExecutor
                8,  // 最大线程数
                60, // 空闲时间
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(500), // 队列容量
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()// 拒绝策略
        );
    }


}
